package com.jarvis.pzz.modules.login;


import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.jarvis.pzz.MainActivity;
import com.jarvis.pzz.R;
import com.jarvis.pzz.common.BaseActivity;
import com.jarvis.pzz.common.ResponseData;
import com.jarvis.pzz.models.AdvModel;
import com.jarvis.pzz.models.ShopsListModel;
import com.jarvis.pzz.seervers.RequestService;
import com.jarvis.pzz.util.JCountDownTimer;
import com.jarvis.pzz.util.net.RequestApi;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class WelcomeActivity extends BaseActivity {
    @BindView(R.id.tv_count)
    TextView tv_count;
    @BindView(R.id.img_wel)
    ImageView img_wel;
    @BindView(R.id.rel_count)
    RelativeLayout rel_count;
    private RequestService service;
    private MyCountDown countDown;
    @Override
    public int getLayoutId() {
        return R.layout.activity_welcom;
    }

    @Override
    public void initData() {
        service = RequestApi.create(RequestService.class);
        showDialog("loading");
        Call<ResponseData<AdvModel>> adv = service.getAdv();
        adv.enqueue(new Callback<ResponseData<AdvModel>>() {
            @Override
            public void onResponse(Call<ResponseData<AdvModel>> call, Response<ResponseData<AdvModel>> response) {
                disMiss();
                if (response.body() != null) {
                    int resultCode = response.body().getResultCode();
                    if (resultCode == 200) {
                        AdvModel data = response.body().getData();
                        countDown = new MyCountDown(data.getCloseTime()*1000, 1000);
                        countDown.start();
                        Glide.with(WelcomeActivity.this).load(data.getAdvLink()).error(R.drawable.welcome).into(img_wel);
                    } else {
                        Toast.makeText(getApplicationContext(),response.body().getMessage(),Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getApplicationContext(),"请求失败",Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(Call<ResponseData<AdvModel>> call, Throwable t) {
                disMiss();
                Toast.makeText(getApplicationContext(), RequestApi.detailError(t),Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    protected void onStart() {
        super.onStart();
        if (countDown != null && countDown.isRunning()) {
            countDown.reStart();

        }
    }

    @Override
    protected void onStop() {

        if (countDown != null && countDown.isRunning()) {
            countDown.pause();

        }
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        if (countDown != null) {
            countDown.cancel();
        }
        super.onDestroy();
    }
    @Override
    public void initView(){
        rel_count.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (countDown != null) {
                    countDown.cancel();
                }
                startActivity(MainActivity.class);
                finish();
            }
        });
    }

    public class MyCountDown extends JCountDownTimer {

        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public MyCountDown(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tv_count.setText(millisUntilFinished / 1000+"s");
        }

        @Override
        public void onFinish() {
            startActivity(MainActivity.class);
            finish();
        }
    }
}
